package org.mixed.core.registry.dynamic;

import org.mixed.core.registry.DynamicRegistry;
import org.mixed.core.registry.entity.DataSourceEntity;
import org.mixed.script.modules.db.datasource.DynamicDataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;

import javax.sql.DataSource;

public class DataSourceDynamicRegistry implements DynamicRegistry<DataSourceEntity> {
    private final DynamicDataSource dynamicDataSource;

    public DataSourceDynamicRegistry(DynamicDataSource dynamicDataSource) {
        this.dynamicDataSource = dynamicDataSource;
    }

    @Override
    public void register(DataSourceEntity entity) {
        DataSource dataSource = DataSourceBuilder.create()
                .driverClassName(entity.getDriverClassName())
                .url(entity.getUrl())
                .username(entity.getUsername())
                .password(entity.getPassword())
                .build();
        dynamicDataSource.getResolvedDataSources().put(entity.getKey(), dataSource);
    }

    @Override
    public void unRegister(DataSourceEntity entity) {

    }
}
